//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    public class Text : ViewModelBase, IOfficeXmlSerializable
    {
        private TextBody _rich;
        private StringReference _strRef;

        public StringReference Reference
        {
            get { return _strRef; }
            set { _strRef = value; notifyPropertyChanged("Reference");
            notifyPropertyChanged("Content");
            }
        }

        public TextBody TextProperties
        {
            get { return _rich; }
            set { _rich = value;
            notifyPropertyChanged("TextProperties");
            notifyPropertyChanged("Content");
            }
        }

        public object Content
        {
            get
            {
                if (_strRef != null)
                    return _strRef;
                else
                    return TextProperties;
            }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("tx"))
            {
                c.PassAttributes();
                switch (c.ReaderLocalName)
                {
                    case "strRef":
                        Reference = StringReference.ReadStringReference(c); //\\c.ReadElement<StringReference>());
                     //\\   throw new NotImplementedException();
                        //\\Reference = c.ReadElement<StringReference>(delegate { return new StringReference(); });
//                        throw new NotImplementedException(); //\\Reference = new StringReference();
                        //                      Reference.ReadXml(c);
                        break;
                    case "rich":
                        TextProperties = c.ReadElementAs<TextBody>("rich");
                        break;
                    default:
                        throw new NotSupportedException(c.Reader.LocalName);
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("tx"))
            {
                if (Reference != null)
                    c.WriteElement(Reference);
                else
                    c.WriteElementAs(TextProperties, "rich");
            }
        }

        #endregion

    }
}